package com.gitee.qdbp.general.common.biz.oplog.service;

import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.general.common.api.oplog.model.AccessRecordBean;
import com.gitee.qdbp.general.common.api.oplog.model.AccessRecordUpdate;
import com.gitee.qdbp.general.common.api.oplog.service.IAccessRecordExecutor;
import com.gitee.qdbp.general.common.biz.oplog.basic.AccessRecordBasic;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 访问日志业务处理类
 *
 * @author zhh
 * @version 171129
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class AccessRecordExecutor implements IAccessRecordExecutor {

    /** 日志对象 **/
    private static final Logger log = LoggerFactory.getLogger(AccessRecordExecutor.class);

    /** 访问日志DAO **/
    @Autowired
    private AccessRecordBasic accessRecordBasic;

    @Override
    public void create(AccessRecordBean model) throws ServiceException {
        String msg = "Failed to create AccessRecord. ";

        if (VerifyTools.isBlank(model)) {
            log.error(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 业务逻辑
        model.setId(null); // 主键由Basic生成, 不允许外部传入

        // 向sys_access_record表插入记录
        accessRecordBasic.create(model);

    }

    @Override
    public void update(AccessRecordUpdate model) throws ServiceException {
        String msg = "Failed to update AccessRecord. ";

        if (VerifyTools.isBlank(model)) {
            log.error(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        String id = model.getId();
        if (VerifyTools.isBlank(id) && model.getWhere() != null) {
            id = model.getWhere().getId();
        }

        // 这些字段不允许修改
        // model.setId(this.getId()); // 主键
        model.setTenantCode(null); // 租户编号
        model.setUserId(null); // 用户ID
        // model.setUserName(this.getUserName()); // 用户名
        model.setIp(null); // IP
        // model.setCity(this.getCity()); // 城市
        model.setRequestProtocol(null); // 请求协议
        model.setRequestUrl(null); // 请求URL
        model.setRequestParams(null); // 请求参数
        // model.setRequestDesc(this.getRequestDesc()); // 请求描述
        model.setRefererUrl(null); // 来源页
        model.setEntrance(null); // 入口
        // model.setRefererType(this.getRefererType()); // 来源类型
        // model.setRefererKeyword(this.getRefererKeyword()); // 来源关键词
        model.setUserAgent(null); // UserAgent
        // model.setBrowserType(this.getBrowserType()); // 浏览器类型
        // model.setBrowserVersion(this.getBrowserVersion()); // 浏览器版本
        // model.setOsType(this.getOsType()); // 操作系统类型
        // model.setOsVersion(this.getOsVersion()); // 操作系统版本
        // model.setDeviceType(this.getDeviceType()); // 设备类型
        model.setCreateTime(null); // 创建时间
        // model.setOptions(this.getOptions()); // 选项

        accessRecordBasic.update(model, false);
    }

    @Override
    public void delete(String id) throws ServiceException {
        String msg = "Failed to delete AccessRecord. ";

        if (VerifyTools.isBlank(id)) {
            log.error(msg + "params is null: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 从sys_access_record表查询原记录
        AccessRecordBean older = accessRecordBasic.findById(id);
        if (older == null) {
            log.error(msg + "record is not found.\n\tid is " + id);
            throw new ServiceException(ResultCode.RECORD_NOT_EXIST);
        }

        // 删除sys_access_record表的记录
        accessRecordBasic.deleteByIds(Arrays.asList(id), false);
    }

    @Override
    public void delete(List<String> ids) throws ServiceException {
        String msg = "Failed to delete AccessRecord. ";

        if (VerifyTools.isBlank(ids)) {
            log.error(msg + "params is null: ids");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 删除sys_access_record表的记录
        accessRecordBasic.deleteByIds(ids, false);
    }

}
